/*
 * Home.java
 *
 * Created on May 25, 2005, 11:27 AM
 */
package ge.ktapestry.pages;

import ge.info.domain.DbExecutorIface;
import ge.info.domain.Service;



import ge.info.logging.Session;
import ge.info.sip.SessionMap;
import org.apache.tapestry.PageRedirectException;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.bean.EvenOdd;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageValidateListener;
import org.apache.tapestry.valid.ValidationDelegate;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public abstract class BasePageExt extends BasePageWithExceptions implements PageValidateListener
{

    @InjectObject("spring:CurrentSessions")
    public abstract SessionMap getCurrentSessions();

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getRequest();

    @Bean
    public abstract ValidationDelegate getDelegate();

    @InjectObject("spring:dbExecutor")
    public abstract DbExecutorIface getDbExecutor();

    
    

    @InjectState("services")
    public abstract List getServices();

    public abstract void setServices(List services);

    @Bean
    public abstract EvenOdd getEvenOdd();
    private boolean odd;

    public void pageValidate(PageEvent event)
    {
        if (getSession() == null || getSession().getUserName() == null)
        {
            throw new PageRedirectException("Home");
        }
    }

    public boolean isOdd()
    {
        odd = !odd;
        return odd;
    }

    public void setOdd(boolean odd)
    {
        this.odd = odd;
    }

    public List<Service> getServiceList()
    {
        List list = getServices();
        if (list == null || list.size() == 0)
        {
            list = getDbExecutor().getServices();
            setServices(list);
        }
        return list;
    }
    public Session getSession()
    {
        return getCurrentSessions().get(getRequest().getRemoteAddr());
    }

    // public abstract void setSession(Session session);
}
